package com.yikome.utils;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.thread.ThreadFactoryBuilder;

public class CustomerThreadUtils {

	private static ExecutorService executor;

	private CustomerThreadUtils() {
	}
	
	private synchronized static ExecutorService init() {
		
		int processors = Runtime.getRuntime().availableProcessors();
		
		if(executor == null) {
			executor = new ThreadPoolExecutor(processors, 
					processors * 2, 
					100, TimeUnit.MILLISECONDS, 
					new ArrayBlockingQueue<>(processors * 8),
					new ThreadFactoryBuilder().setNamePrefix("customer-thread-").build(),
					new ThreadPoolExecutor.CallerRunsPolicy());
		}
		
		return executor ;
	}
	
	public static void execute(Runnable runnable) {
		init().execute(runnable);
	}
	
	public static void shutdown() {
		init().shutdown();
	}
}
